//
//  AppDelegate.m
//  XMLYFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "AppDelegate.h"
#import "FindController.h"
#import "ListenViewController.h"
#import "downController.h"
#import "MineController.h"
#import "ZHPlayViewController.h"
#import "ZHPlayButtonView.h"
#import "UIButton+WebCache.h"

@interface AppDelegate ()

@property (nonatomic,strong)ZHTabBarController *tabBarC;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // TZ  SDK短信验证
    [SMSSDK registerApp:@"e1315e6eea14"
             withSecret:@"7e70139ef6505ba19db7253b16f46957"];
    
    
    
    
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
#pragma mark - 设置UINavigationController
    // 发现
    FindController *findVC = [[FindController alloc] init];
    BaseNavigationController *findNaVC = [[BaseNavigationController alloc] initWithRootViewController:findVC];
    
    
    findNaVC.navigationBar.translucent = NO;
    [findNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"50700-1.jpg"] forBarMetrics:UIBarMetricsDefault];
    
    // 定制听
    ListenViewController *listenVC = [[ListenViewController alloc] init];
    BaseNavigationController *listenNaVC = [[BaseNavigationController alloc] initWithRootViewController:listenVC];
    listenNaVC.navigationBar.translucent = NO;
    
    
    // 分类听
    downController *downVC = [[downController alloc] init];
    BaseNavigationController *downNaVC = [[BaseNavigationController alloc] initWithRootViewController:downVC];
    downNaVC.navigationBar.translucent = NO;
    
    
    // 我的
    MineController *mineVC = [[MineController alloc] init];
    BaseNavigationController *mineNaVC = [[BaseNavigationController alloc] initWithRootViewController:mineVC];
    mineNaVC.navigationBar.translucent = NO;
    
    //
    ZHPlayViewController *playVC = [[ZHPlayViewController alloc]init];
    
#pragma mark - 设置tabBar
    self.tabBarC = [[ZHTabBarController alloc] init];
    _tabBarC.viewControllers = [NSArray arrayWithObjects:findNaVC, listenNaVC, playVC, downNaVC, mineNaVC, nil];
    _tabBarC.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_bg@2x.png"];
    _tabBarC.tabBar.alpha = 1;

    self.window.rootViewController = _tabBarC;

#pragma mark - 创建数据库
    [[ZHFMDB shareFMDB] openFMDBAndCreateTable];
    [[ZHPlayButtonView shareButton].musicBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:[[[ZHFMDB shareFMDB] selectTrackImageData] lastObject]] forState:UIControlStateNormal placeholderImage:PLACEHOLDER_IMAGE];
//    NSLog(@"[[[ZHFMDB shareFMDB] selectTrackImageData] lastObject]=%@",[[[ZHFMDB shareFMDB] selectTrackImageData] lastObject]);
    [ZHPlayViewController sharaMusicPlayer].song_id = [[[[ZHFMDB shareFMDB] selectTrackIDData] lastObject] trackId];
#pragma mark - 上次播放提示框
    if ([[ZHFMDB shareFMDB] selectTrackNameData].count > 0) {
        UIButton *messageLabel = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH_ZH/2, SCREEN_HEIGHT_ZH - self.tabBarC.tabBar.frame.size.height-10, SCREEN_HEIGHT_ZH/4, SCREEN_HEIGHT_ZH*0.03)];
        messageLabel.backgroundColor = [UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1];
        messageLabel.alpha = 0.8;
        messageLabel.titleLabel.font = [UIFont systemFontOfSize:13];
        messageLabel.titleLabel.textColor = [UIColor lightGrayColor];
        messageLabel.layer.masksToBounds = YES;
        messageLabel.layer.cornerRadius = 5;
        [messageLabel setTitle:[NSString stringWithFormat:@"上次播放:%@",[[[ZHFMDB shareFMDB] selectTrackNameData] lastObject]] forState:UIControlStateNormal ] ;
        [messageLabel sizeToFit];
        //        messageLabel.titleLabel.
        messageLabel.center = CGPointMake(SCREEN_WIDTH_ZH/2,  SCREEN_HEIGHT_ZH - self.tabBarC.tabBar.frame.size.height-40);
        [self.window addSubview:messageLabel];
        [UILabel animateWithDuration:2 delay:2 options:UIViewAnimationOptionLayoutSubviews animations:^{
            
            messageLabel.alpha = 0;
        } completion:^(BOOL finished) {
            
            
        }];

    }

    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
